using System;


namespace Slf.Samples.DelegateLoggerSample
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.ForegroundColor = ConsoleColor.Green;
      Console.Out.WriteLine("If a DelegateLogger is plugged into SLF, it invokes a delegate method you define at runtime.");
      Console.Out.WriteLine("This sample analyzes the LogLevel at runtime and adjusts the console color accordingly.\n\n");
      Console.ResetColor();

      //the delegate logger will invoke the method passed to
      //it through its constructor
      DelegateLogger logger = new DelegateLogger(WriteColoredOutput);
      LoggerService.SetLogger(logger);

      CreateLogEntries();
      Console.ReadLine();
    }


    private static void CreateLogEntries()
    {
      ILogger logger = LoggerService.GetLogger();

      logger.Debug("This is debug output.");
      logger.Info("This is an information.");
      logger.Warn("This is a warning.");
      logger.Error("This is an error.");
      logger.Fatal("This is a fatal error.");
    }



    //this method is invoked by the DelegateLogger
    //it sets the console color based on the item's severity,
    //then writes the log message to the console
    private static void WriteColoredOutput(LogItem item)
    {
      switch(item.LogLevel)
      {
        case LogLevel.Info:
          Console.ForegroundColor = ConsoleColor.Green;
          break;
        case LogLevel.Warn:
          Console.ForegroundColor = ConsoleColor.Cyan;
          break;
        case LogLevel.Error:
          Console.ForegroundColor = ConsoleColor.Yellow;
          break;
        case LogLevel.Fatal:
          Console.ForegroundColor = ConsoleColor.Red;
          break;
        default:
          Console.ResetColor();
          break;
      }

      Console.Out.WriteLine(item.Message);
    }
  }
}